package com.dk.smsexpress.server.imp;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dk.smsexpress.common.base.IMobiSmsManualSelectAccount;
import com.dk.smsexpress.common.base.MobiAccount;
import com.dk.smsexpress.common.exception.ExceptionConstant;
import com.dk.smsexpress.common.exception.SmsException;
import com.dk.smsexpress.server.utils.Formatter;

public class MobiSmsManualSelectAccount implements IMobiSmsManualSelectAccount {

	final static Logger logger = LoggerFactory
			.getLogger(MobiSmsAutoSelectAccount.class);

	private MobiSms mobiSms = null;

	@Override
	public void SendMobiSMS(String username, String password, String phoneNum,
			String message) throws SmsException, IOException {
		MobiAccount account = new MobiAccount(username, password);

		mobiSms = new MobiSms(account);

		if (!Formatter.isValidPhoneNumber(phoneNum)) {
			throw new SmsException(ExceptionConstant.INVALID_PHONE_NUMBER);
		}
		phoneNum = Formatter.parsePhoneNumber(phoneNum);

		mobiSms.setPhoneNum(phoneNum);
		mobiSms.setMessage(message);
		if (mobiSms.getMessageLeft() > 0) {
			mobiSms.sendMessage();
		} else {
			throw new SmsException(ExceptionConstant.OUT_OFF_MESSAGE);
		}
	}

}
